#ifndef HOSTCOMPUTERMSG_H
#define HOSTCOMPUTERMSG_H
/***
 * 和上位机通信类
 ***/
#include <QObject>
#include <QUdpSocket>
#include <QThread>
#include <QDateTime>

#include "head.h"

class UdpWork;

class HostComputerMsg : public QObject
{
    Q_OBJECT
public:
    explicit HostComputerMsg(QString ip, quint16 listenPort, QString destIp,
                             quint16 destPort, QObject *parent = nullptr);
    ~HostComputerMsg();

private:
    UdpWork *m_udpWork;
    QThread *m_thread;
    QString m_listenIP; //监听IP
    quint16 m_listport; //监听port
    QString m_destIP;   //目标IP
    quint16 m_destPort; //目标port

public slots:
    void onStartStoringResp(CameraMethod method);   //开始存储上位机反馈
    void onStopStoringResp(CameraMethod method); //停止存储上位机反馈

    void onQueryStatusResp(bool midWaveIR, bool longWaveIR, bool visible, bool aerial);    //上报状态
    void onQueryVideoNameResp(const QStringList &videoNames);    //上报视频名称
    void onDeleteVideoResp(const QString &videoName);       //删除视频回复
    void onTimingResp();       //授时回复
    void onAllDiskFull();       //所有磁盘满了

signals:
    void sigSendData(QByteArray data, QString m_destIP, quint16 m_destPort);    //给UdpWork发送发送指令

    void sigStartStoring(CameraMethod); //CameraMethod 开始存储
    void sigStopStoring(CameraMethod);  //CameraMethod停止存储
    void sigQueryStatus();              //查询状态指令
    void sigQueryVideo();               //查询视频
    void sigDeleteVideo(QString);       //删除视频
    void sigTiming(QDateTime);          //授时
    void sigTochangeH264Time(quint8 time); //h264时长设置
};

class UdpWork : public QObject
{
    Q_OBJECT
public:
    UdpWork(QString ip, quint16 listenPort, QString destIp,
            quint16 destPort, QObject *parent = nullptr) : QObject(parent){
        m_listenIP = ip;
        m_listport = listenPort;
        m_destIP = destIp;
        m_destPort = destPort;
    }

    void init();        //初始化

private:
    QUdpSocket *m_udpSocket;

    void parseCommand(QByteArray cmd);  //处理收到的指令

    QString m_listenIP; //监听IP
    quint16 m_listport; //监听port
    QString m_destIP;   //目标IP
    quint16 m_destPort; //目标port

public slots:
    void onReceive();
    int onSendData(QByteArray cmd, QString detIP, quint16 disPort);       //给上位机发送消息

signals:
    void sigStartStoring(CameraMethod); //CameraMethod 开始存储
    void sigStopStoring(CameraMethod);  //CameraMethod停止存储
    void sigQueryStatus();          //查询状态指令
    void sigQueryVideo();           //查询视频
    void sigDeleteVideo(QString);   //删除视频
    void sigTiming(QDateTime);      //授时
    void sigTochangeH264Time(quint8 time); //h264时长设置
};

#endif // HOSTCOMPUTERMSG_H
